package com.logicbus.dbcp.xscript;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.*;
import org.apache.commons.lang3.StringUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * jdbc直连
 *
 * @since 1.6.12.22 [20190215 duanyy] <br>
 */
public class JdbcConn extends Segment {

    protected String $dbconn = "dbconn";
    protected boolean autoCommit = true;
    /**
     * jdbc驱动
     */
    protected String $driver = "com.mysql.jdbc.Driver";

    /**
     * URL
     */
    protected String $url = "jdbc:mysql://localhost:3306/mysql";

    /**
     * username
     */
    protected String $username;

    /**
     * password
     */
    protected String $password;

    public JdbcConn(String tag, Logiclet p) {
        super(tag, p);

        registerModule("db-direct",JdbcConn.class);
        registerModule("db",DBConn.class);
        registerModule("db-commit",Commit.class);
        registerModule("db-delete",Delete.class);
        registerModule("db-update",Update.class);
        registerModule("db-list",ListAll.class);
        registerModule("db-rollback",Rollback.class);
        registerModule("db-scan",Scan.class);
        registerModule("db-new",New.class);
        registerModule("db-select",Select.class);
        registerModule("db-query",Query.class);
        registerModule("db-keyvalues",KeyValues.class);
    }

    @Override
    public void configure(Properties p) {
        super.configure(p);
        $driver = PropertiesConstants.getRaw(p,"driver", "");
        $url = PropertiesConstants.getRaw(p,"url", "");
        $username = PropertiesConstants.getRaw(p,"username", "");
        $password = PropertiesConstants.getRaw(p,"password", "");
        $dbconn = PropertiesConstants.getRaw(p,"dbconn", "");
        autoCommit = PropertiesConstants.getBoolean(p, "autoCommit",autoCommit);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        String dbconn = PropertiesConstants.transform(ctx,$dbconn,"dbconn");

        if (StringUtils.isNotEmpty(dbconn)) {
            Connection conn = ctx.getObject(dbconn);
            if (conn == null) {
                String driver = PropertiesConstants.transform(ctx,$driver,"");
                String url = PropertiesConstants.transform(ctx,$url,"");
                String username = PropertiesConstants.transform(ctx,$username,"");
                String password = PropertiesConstants.transform(ctx,$password,"");
                try {
                    ClassLoader cl = Settings.getClassLoader();
                    Class.forName(driver, true, cl);
                    conn = DriverManager.getConnection(url, username, password);
                }catch (Exception ex){
                    throw new BaseException("core.e1300", ex.getMessage());
                }
                if (conn != null) {
                    try {
                        conn.setAutoCommit(autoCommit);
                        ctx.setObject(dbconn, conn);
                        super.onExecute(root, current, ctx, watcher);
                    } catch (SQLException ex) {
                        throw new BaseException("core.e1300", ex.getMessage());
                    } finally {
                        IOTools.close(conn);
                    }
                }
            } else {
                //连接已经存在
                super.onExecute(root, current, ctx, watcher);
            }
        }
    }
}
